<template>
  <!-- R3价格下发 -->
  <div>
    <TopFilter :list="topFilterList" :cols="2" :labelWidth="100" :rows="2" :collapse="false" @filterChange="handleSearch"></TopFilter>
    <FilterTable
      ref="table"
      columnsRef="partPartInfoManagePartInfoR3PriceDownload"
      height="auto"
      maxHeight="350"
      :columns="columns"
      :fetchapi="fetchapi"
      :params="fetchParams"
      :datakey="'rows'"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="handleTableRowSelect"
      :onPageChange="handleTablePageChange"
    ></FilterTable>
    <div class="bottomBtnWrap">
      <el-button @click="close">取 消</el-button>
      <AjaxButton type="primary" :click="handleDownLoadR3Price">下载R3价格</AjaxButton>
      <AjaxButton type="primary" :click="handleReplaceClick">更替价格</AjaxButton>
      <AjaxButton type="primary" :click="handleReplaceAll">全量下发</AjaxButton>
    </div>
  </div>
</template>

<script>
import { queryR3PartPriceList, updateR3PartPriceList, downloadR3Price, updateR3PartPriceListAll } from '@/api/part/partInfoManage/partInfo';
import { notifyAction, messageAction, confirmAction } from '@/utils';

export default {
  data() {
    return {
      topFilterList: this.createTopFilterList(),
      columns: this.createTableColumns(),
      fetchapi: queryR3PartPriceList,
      fetchParams: { xhrAbort: false },
      selectedTableRowData: [], // 选中的table行项
      pageSize: 20 // 每页显示条数(判断当前页面是否全选)
    };
  },
  methods: {
    // 全量替换
    async handleReplaceAll() {
      let dataLength = this.selectedTableRowData.length;
      // if (dataLength) {
      let queryType = '';
      let partNoArr = [],
        idArr = [];
      // if (dataLength == 0) {
      //   queryType = 1;
      // } else {
      //   queryType = 2;
      //   partNoArr = this.selectedTableRowData.map(item => item.partNo);
      //   idArr = this.selectedTableRowData.map(item => item.id);
      // }
      let res = await updateR3PartPriceListAll({
        queryType
        // partNo: partNoArr.join(','),
        // id: idArr.join(',')
      });
      if (res.resultCode == 200) {
        notifyAction('修改成功');
        this.requeryTableData();
      }
    },
    createTopFilterList() {
      return [
        {
          type: 'RANGE_DATE',
          label: '价格调整日期',
          fieldName: 'adjustDate'
        },
        {
          type: 'INPUT',
          label: '配件代码',
          fieldName: 'partNo'
        },
        {
          type: 'RANGE_DATE',
          label: '出库日期',
          fieldName: 'outDate'
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '配件代码',
          dataIndex: 'partNo',
          showOverflowTooltip: true
        },
        {
          title: '配件名称',
          width: 150,
          dataIndex: 'partName',
          showOverflowTooltip: true
        },
        {
          title: '原不含税采购价',
          dataIndex: 'planPrice'
        },
        {
          title: '原不含税销售价',
          dataIndex: 'notTaxCostSalesPrice'
        },
        {
          title: '原不含税出库价',
          dataIndex: 'notTaxCostOutPrice'
        },
        {
          title: '新不含税采购价',
          dataIndex: 'kbetr'
        },
        {
          title: '新不含税销售价',
          dataIndex: 'zxsdj'
        },
        {
          title: '新不含税出库价',
          dataIndex: 'zckdj'
        },
        {
          title: '状态',
          dataIndex: 'partStockStatus'
        },
        {
          title: '加价率',
          dataIndex: 'priceAddRate'
        },
        {
          title: '价格调整日期',
          dataIndex: 'zxfrq'
        },
        {
          title: '车型代码',
          dataIndex: 'model',
          showOverflowTooltip: true
        },
        {
          title: '车型名称',
          dataIndex: 'brandName',
          showOverflowTooltip: true
        }
      ];
    },
    // 点击搜索或者重置
    handleSearch(e) {
      this.fetchParams.xhrAbort = true;
      this.fetchParams = {
        partNo: e.partNo || '',
        outDateBefore: e.outDate[0] || '',
        outDateAfter: e.outDate[1] || '',
        adjustDateBefore: e.adjustDate[0] || '',
        adjustDateAfter: e.adjustDate[1] || '',
        xhrAbort: false
      };
    },
    // table行项选择
    handleTableRowSelect(e) {
      this.selectedTableRowData = e;
    },
    // table分页变化
    handleTablePageChange(e) {
      this.pageSize = e.pageSize;
    },
    // 点击更替价格
    async handleReplaceClick() {
      let dataLength = this.selectedTableRowData.length;
      // if (dataLength) {
      let queryType = '';
      let partNoArr = [],
        idArr = [];
      if (dataLength == 0) {
        queryType = 1;
      } else {
        queryType = 2;
        partNoArr = this.selectedTableRowData.map(item => item.partNo);
        idArr = this.selectedTableRowData.map(item => item.id);
      }
      let res = await updateR3PartPriceList({
        queryType,
        partNo: partNoArr.join(','),
        id: idArr.join(',')
      });
      if (res.resultCode == 200) {
        notifyAction('修改成功');
        this.requeryTableData();
      }
      // } else {
      //   messageAction('请选择需要更新的配件');
      // }
    },
    // 点击下载r3价格
    async handleDownLoadR3Price() {
      let dataLength = this.selectedTableRowData.length;
      // if (dataLength) {
      let queryType = '';
      let partNoArr = [],
        idArr = [];

      if (dataLength == 0) {
        queryType = 1;
      } else {
        queryType = 2;
        partNoArr = this.selectedTableRowData.map(item => item.partNo);
        idArr = this.selectedTableRowData.map(item => item.id);
      }
      let res = await downloadR3Price({
        queryType,
        partNo: partNoArr.join(','),
        id: idArr.join(',')
      });
      if (res.resultCode == 200) {
        notifyAction('下载成功');
        this.requeryTableData();
      }
      // } else {
      //   messageAction('请选择需要进行下载的配件');
      // }
    },
    // 重新查询
    requeryTableData() {
      this.fetchParams.xhrAbort = true;
      this.fetchParams.xhrAbort = false;
    },
    close() {
      this.$emit('close');
    },
    // 设置查询日期初始值
    setSearchParamInitalValue() {
      let date = new Date();
      let thisMonth = date.getMonth() + 1;
      let thisYear = date.getFullYear();

      let monthFirstDay = new Date(thisYear + '/' + thisMonth + '/1');
      this.topFilterList.find(item => item.fieldName == 'adjustDate').initialValue = [monthFirstDay, new Date()];
      this.topFilterList.find(item => item.fieldName == 'outDate').initialValue = [monthFirstDay, new Date()];
    }
  }
};
</script>

<style scoped>
.bottomBtnWrap {
  position: absolute;
  left: 0;
  bottom: 0;
  right: 0;
  z-index: 9;
  border-top: 1px solid #e9e9e9;
  padding: 10px 20px;
  background: #fff;
  text-align: right;
}
</style>
